﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.CustomerManagement
{
    public partial class SalesActive : BasePage
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!Page.IsPostBack)
            {
                this.txtDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                // 按钮权限控制
                //ControlAuthority(new List<LinkButton> { btnAdd, btnUpdate, btnDelete }, 3);
                //LoadData();
            }
        }

        [WebMethod]
        public static string GetDatas(string code, string type, string date, string orderField, string orderValue, int pageIndex, int pagePerSize)
        {
            string strResult;
            string strWhere = "";
            if (!string.IsNullOrEmpty(code))
                strWhere += string.Format(" and tca.FNumber like '%{0}%'", code);
            if (type != "0")
                strWhere += string.Format(" and tca.FAddress={0}", type);
            if (!string.IsNullOrEmpty(date))
            {
                strWhere += string.Format(" and convert(char(10),tca.FOppDate,120)='{0}'", date);
            }
            ICustActivitiesBLL actBLL = (ICustActivitiesBLL)IocContainer.Instance.Resolve<ICustActivitiesBLL>();
            //DataTable dt = bussBLL.Query(strWhere);
            StringBuilder strOrderBy = new StringBuilder();
            strOrderBy.Append(!string.IsNullOrEmpty(orderField) ? " " + orderField : "  FDate  ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderValue) ? " " + orderValue : "  desc ");
            int totalRecords = 0;
            int totalPages = 0;
            DataTable dt = actBLL.Query(strWhere, strOrderBy.ToString(), pagePerSize, pageIndex, out totalRecords);
            totalPages = (totalRecords + pagePerSize - 1) / pagePerSize;
            strResult = SerializationHelper.DataTable2Json(dt);
            return "{\"totalRecords\":\"" + totalRecords.ToString() + "\" ,\"totalPages\":\"" + totalPages.ToString() + "\",\"rows\":" +
                   strResult + " }";
        }

        [WebMethod]
        public static Boolean Delete(int id)
        {

            Boolean result = false;
            try
            {
                ICustActivitiesBLL actBLL = (ICustActivitiesBLL)IocContainer.Instance.Resolve<ICustActivitiesBLL>();
                actBLL.Delete(id);
                result = true;
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
            return result;
        }

        //private void LoadData()
        //{
        //    try
        //    {
        //        StringBuilder strs = new StringBuilder();
        //        string strWhere = "";
        //        if (!string.IsNullOrEmpty(this.txtCode.Text.Trim()))
        //            strWhere += string.Format(" and tca.FNumber like '%{0}%'", this.txtCode.Text.Trim());
        //        if (this.ddlstType.SelectedValue != "0")
        //            strWhere += string.Format(" and tca.FAddress={0}", this.ddlstType.SelectedValue);
        //        if (!string.IsNullOrEmpty(this.txtDate.Text.Trim()))
        //        {
        //            strWhere += string.Format(" and convert(char(10),tca.FOppDate,120)='{0}'", this.txtDate.Text.Trim());
        //        }
        //        DataTable dt = actBLL.Query(strWhere);
        //        if (dt != null && dt.Rows.Count > 0)
        //        {
        //            foreach (DataRow dataRow in dt.Rows)
        //            {
        //                strs.AppendFormat("<tr rel='{0}'>", dataRow["FItemId"]);
        //                strs.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", dataRow["FNumber"], dataRow["BssCode"], dataRow["FType"]);
        //                strs.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", dataRow["FOppDate"], dataRow["FNote"], dataRow["EmpName"]);
        //                strs.AppendFormat("<td>{0}</td><td>{1}</td>", dataRow["FDate"], dataRow["FSubmitDate"]);
        //                strs.Append("</tr>");
        //            }
        //        }
        //        litActiveList.Text = strs.ToString();
        //    }
        //    catch (Exception exception)
        //    {
        //        Logger.Instance.Error(exception);
        //    }
        //}

        //protected void btnSearch_Click(object sender, EventArgs e)
        //{
        //    LoadData();
        //}

        //protected void btnhidDelete_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        int Id = 0;
        //        int.TryParse(this.selectedActiveID.Value.Trim(), out Id);
        //        actBLL.Delete(Id);
        //        LoadData();
        //    }
        //    catch (Exception exception)
        //    {
        //        Logger.Instance.Error(exception);
        //    }
        //}
    }
}